package com.zhtkj.service;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.joda.time.DateTime;
import org.joda.time.Seconds;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhtkj.constant.AccessData;
import com.zhtkj.constant.Constant;
import com.zhtkj.constant.FenceConstant;
import com.zhtkj.dto.CarAnalysis;
import com.zhtkj.dto.Trip;
import com.zhtkj.entity.gateway.CarHistory;
import com.zhtkj.entity.zhatu.Car;
import com.zhtkj.entity.zhatu.CarTrip;
import com.zhtkj.entity.zhatu.Consapp;
import com.zhtkj.entity.zhatu.UnloadingArea;
import com.zhtkj.mapper.zhatu.CarMapper;
import com.zhtkj.mapper.zhatu.CarTripMapper;
import com.zhtkj.mapper.zhatu.ConsappMapper;
import com.zhtkj.mapper.zhatu.UnloadingAreaMapper;
import com.zhtkj.util.GeoUtil;
import com.zhtkj.util.Point;

/** 
* @ClassName: TripService 
* @Description: 趟次分析对象
* @author mafeng 
* @date 2017年9月7日 上午9:50:35 
*  
*/
@Service
public class TripService {

	@Autowired
	private CarMapper carMapper;
	
	@Autowired
	private CarTripMapper carTripMapper;
	
	@Autowired
	private ConsappMapper consappMapper;
	
	@Autowired
	private UnloadingAreaMapper unloadingAreaMapper;
	
	//趟次分析
	@Transactional(propagation = Propagation.NEVER)
	public void tripAnalysis(AccessData accessData, CarAnalysis carAnalysis) {
		List<CarHistory> carHistorys = carAnalysis.getCarHistoryList();
		Trip trip = carAnalysis.getTrip();
		CarHistory lastCarHistory = carAnalysis.getLastCarHistory();
		FenceConstant fenceConstant = accessData.getFenceConstant();
		Map<String, List<Long>> consappIdMap = accessData.getCardConstant().getConsappIdMap();
		Map<String, List<Long>> unloadingIdMap = accessData.getCardConstant().getUnloadingIdMap();
		
		for (int i = 0; i < carHistorys.size(); i++) {
			CarHistory prevCarHistory = (i == 0) ? lastCarHistory : carHistorys.get(i - 1);
			CarHistory currCarHistory = carHistorys.get(i);
			if (prevCarHistory != null && currCarHistory != null) {
				Point prevPoint = new Point(prevCarHistory.getGpsPosX(), prevCarHistory.getGpsPosY());
				Point currPoint = new Point(currCarHistory.getGpsPosX(), currCarHistory.getGpsPosY());
				
				// 判断是否进入或者离开工地
				Iterator<Entry<Long, List<Point>>> consappIt = fenceConstant.getConsappInfoMap().entrySet().iterator();
				while (consappIt.hasNext()) {
					Entry<Long, List<Point>> entry = consappIt.next();
					// 如果是进入工地
					if (!GeoUtil.containsPoint(entry.getValue(), prevPoint) && 
						GeoUtil.containsPoint(entry.getValue(), currPoint)) {
						trip.setEnterConsappTemp(currCarHistory);
						trip.setEnterConsappId(entry.getKey());
					}
					// 如果是离开工地
					if (GeoUtil.containsPoint(entry.getValue(), prevPoint) && 
						!GeoUtil.containsPoint(entry.getValue(), currPoint)) {
						if (entry.getKey().equals(trip.getEnterConsappId())) {
							trip.setLeaveConsappTemp(prevCarHistory);
						}
					}
					// 如果在工地范围内
					if (trip.getEnterConsappTemp() != null && trip.getLeaveConsappTemp() == null) {
						trip.setConsappMileage(trip.getConsappMileage() + GeoUtil.getDistance(prevPoint, currPoint).intValue());
					}
				}
				// 如果有工地进出记录
				if (trip.getEnterConsappTemp() != null && trip.getLeaveConsappTemp() != null) {
					DateTime startTime = new DateTime(trip.getEnterConsappTemp().getReceiveTime());
					DateTime endTime = new DateTime(trip.getLeaveConsappTemp().getReceiveTime());
					Integer consappTime = Seconds.secondsBetween(startTime, endTime).getSeconds();
					Float consappSpeed = ((float)trip.getConsappMileage()/(float)consappTime)*3.6F;
					if (consappTime >= Constant.stayConsappTime && consappSpeed <= Constant.stayConsappSpeed) {
						//如果上一次的趟次还没插入到数据库则先插入上一次的趟次
						if (trip.getCarTrip() != null) {
							if (trip.getCarTrip().getTripId() == null) {
								CarTrip lastCarTrip = trip.getCarTrip();
								if (lastCarTrip.getConsappId() == null || lastCarTrip.getUnloadingId() == null) {
									lastCarTrip.setTripSecond(null);
									lastCarTrip.setMileage(null);
								}
								lastCarTrip.setInsertTime(new Date());
								if (trip.getBoxUpTime() != null) {
									lastCarTrip.setBoxupTime(trip.getBoxUpTime());
								}
								if (trip.getBoxUpPlace() != null) {
									lastCarTrip.setBoxupPlace(trip.getBoxUpPlace());
								}
								carTripMapper.insertSelective(lastCarTrip);
								carTripMapper.updateStartGps(lastCarTrip.getTripId(), lastCarTrip.getStartX() + " " + lastCarTrip.getStartY());
							}
							trip.setCarTrip(null);
						}
						
						// 插入新的趟次记录
						CarTrip carTrip = new CarTrip();
						carTrip.setCarNumber(carAnalysis.getCarNumber());
						carTrip.setSimNumber(carAnalysis.getDevPhone());
						carTrip.setConsappId(trip.getEnterConsappId());
						Consapp consapp = consappMapper.selectByPrimaryKey(trip.getEnterConsappId());
						carTrip.setConsappName(consapp.getProName());
						carTrip.setConsappIntime(startTime.toDate());
						carTrip.setConsappOuttime(endTime.toDate());
						carTrip.setConsappMileage(trip.getConsappMileage());
						carTrip.setConsappTime(consappTime);
						carTrip.setConsappSpeed(consappSpeed);
						List<Long> consappIdList = consappIdMap.get(carAnalysis.getCarNumber());
						if (consappIdList == null) {
							carTrip.setConsappHascard(0);
						} else {
							Integer hascard = 0;
							for (Long consappId: consappIdList) {
								if (consappId.equals(trip.getEnterConsappId())) {
									hascard =  1;
								}
							}
							carTrip.setConsappHascard(hascard);
						}
						Car query = new Car();
		            	query.setNumberPlate(prevCarHistory.getCarNumber());
		            	List<Car> cars = carMapper.select(query);
		            	if (cars.size() > 0) {
		            		carTrip.setDriverName(cars.get(0).getCarOwnersName());
		            	}
		            	carTrip.setInsertTime(new Date());
		            	carTrip.setStartTime(endTime.toDate());
		            	if (endTime.getHourOfDay() >= 0 && endTime.getHourOfDay() <= 6) {
		            		Date tripDate = DateTime.parse(endTime.minusDays(1).toString("yyyy-MM-dd"),
		            				DateTimeFormat.forPattern("yyyy-MM-dd")).toDate();
		            		carTrip.setTripDate(tripDate);
		            	} else {
		            		Date tripDate = DateTime.parse(endTime.toString("yyyy-MM-dd"),
		            				DateTimeFormat.forPattern("yyyy-MM-dd")).toDate();
		            		carTrip.setTripDate(tripDate);
		            	}
		            	carTrip.setMileage(0);
		            	carTrip.setMaxSpeed(currCarHistory.getGpsSpeed());
		            	if (trip.getBoxUpTime() != null) {
							carTrip.setBoxupTime(trip.getBoxUpTime());
						}
						if (trip.getBoxUpPlace() != null) {
							carTrip.setBoxupPlace(trip.getBoxUpPlace());
						}
		            	carTripMapper.insertSelective(carTrip);
		            	carTripMapper.updateStartGps(carTrip.getTripId(), trip.getEnterConsappTemp().getGpsPosX() + " " + trip.getEnterConsappTemp().getGpsPosY());
		            	trip.setCarTrip(carTrip);
					}
					trip.setEnterConsappTemp(null);
					trip.setEnterConsappId(null);
					trip.setLeaveConsappTemp(null);
				}
				
				// 判断是否进入或者离开卸区
				Iterator<Entry<Long, List<Point>>> unloadingIt = fenceConstant.getUnloadingInfoMap().entrySet().iterator();
				while (unloadingIt.hasNext()) {
					Entry<Long, List<Point>> entry = unloadingIt.next();
					// 如果是进入卸区
					if (!GeoUtil.containsPoint(entry.getValue(), prevPoint) && 
						GeoUtil.containsPoint(entry.getValue(), currPoint)) {
						trip.setEnterUnloadingTemp(currCarHistory);
						trip.setEnterUnloadingId(entry.getKey());
					}
					// 如果是离开卸区
					if (GeoUtil.containsPoint(entry.getValue(), prevPoint) && 
						!GeoUtil.containsPoint(entry.getValue(), currPoint)) {
						if (entry.getKey().equals(trip.getEnterUnloadingId())) {
							trip.setLeaveUnloadingTemp(prevCarHistory);
						}
					}
					// 如果在卸区范围内
					if (trip.getEnterUnloadingTemp() != null && trip.getLeaveUnloadingTemp() == null) {
						trip.setUnloadingMileage(trip.getUnloadingMileage() + GeoUtil.getDistance(prevPoint, currPoint).intValue());
					}
				}
				// 如果有卸区进出记录
				if (trip.getEnterUnloadingTemp() != null && trip.getLeaveUnloadingTemp() != null) {
					DateTime startTime = new DateTime(trip.getEnterUnloadingTemp().getReceiveTime());
					DateTime endTime = new DateTime(trip.getLeaveUnloadingTemp().getReceiveTime());
					Integer unloadingTime = Seconds.secondsBetween(startTime, endTime).getSeconds();
					Float unloadingSpeed = ((float)trip.getUnloadingMileage()/(float)unloadingTime)*3.6F;
					if (unloadingTime >= Constant.stayConsappTime && unloadingSpeed <= Constant.stayUnloadingSpeed) {
						if (trip.getCarTrip() != null) { //如果趟次记录已经持久化
							CarTrip updateCarTrip = new CarTrip();
							CarTrip lastCarTrip = trip.getCarTrip();
							updateCarTrip.setTripId(lastCarTrip.getTripId());
							lastCarTrip.setUnloadingId(trip.getEnterUnloadingId());
							UnloadingArea unloadingArea = unloadingAreaMapper.selectByPrimaryKey(trip.getEnterUnloadingId());
							updateCarTrip.setUnloadingName(unloadingArea.getUnloadingName());
							updateCarTrip.setUnloadingIntime(startTime.toDate());
							updateCarTrip.setUnloadingOuttime(endTime.toDate());
							updateCarTrip.setUnloadingMileage(trip.getConsappMileage());
							updateCarTrip.setUnloadingTime(unloadingTime);
							updateCarTrip.setUnloadingSpeed(unloadingSpeed);
							List<Long> unloadingIdList = unloadingIdMap.get(carAnalysis.getCarNumber());
							if (unloadingIdList == null) {
								updateCarTrip.setUnloadingHascard(0);
							} else {
								Integer hascard = 0;
								for (Long unloadingId: unloadingIdList) {
									if (unloadingId.equals(trip.getEnterUnloadingId())) {
										hascard =  1;
									}
								}
								updateCarTrip.setUnloadingHascard(hascard);
							}
							updateCarTrip.setEndTime(startTime.toDate());
							updateCarTrip.setTripSecond(Seconds.secondsBetween(new DateTime(lastCarTrip.getStartTime()), startTime).getSeconds());
							if (trip.getBoxUpTime() != null) {
								updateCarTrip.setBoxupTime(trip.getBoxUpTime());
							}
							if (trip.getBoxUpPlace() != null) {
								updateCarTrip.setBoxupPlace(trip.getBoxUpPlace());
							}
							carTripMapper.updateByPrimaryKeySelective(updateCarTrip);
							carTripMapper.updateEndGps(updateCarTrip.getTripId(), trip.getEnterUnloadingTemp().getGpsPosX() + " " + 
									trip.getEnterUnloadingTemp().getGpsPosY());
							trip.setCarTrip(null);
						} else { //如果趟次记录没有持久化则插入一条记录
							CarTrip carTrip = new CarTrip();
							carTrip.setCarNumber(carAnalysis.getCarNumber());
							carTrip.setSimNumber(carAnalysis.getDevPhone());
							carTrip.setUnloadingId(trip.getEnterUnloadingId());
							UnloadingArea unloadingArea = unloadingAreaMapper.selectByPrimaryKey(trip.getEnterUnloadingId());
							carTrip.setUnloadingName(unloadingArea.getUnloadingName());
							carTrip.setUnloadingIntime(startTime.toDate());
							carTrip.setUnloadingOuttime(endTime.toDate());
							carTrip.setUnloadingMileage(trip.getConsappMileage());
							carTrip.setUnloadingTime(unloadingTime);
							carTrip.setUnloadingSpeed(unloadingSpeed);
							List<Long> unloadingIdList = unloadingIdMap.get(carAnalysis.getCarNumber());
							if (unloadingIdList == null) {
								carTrip.setUnloadingHascard(0);
							} else {
								Integer hascard = 0;
								for (Long unloadingId: unloadingIdList) {
									if (unloadingId.equals(trip.getEnterUnloadingId())) {
										hascard =  1;
									}
								}
								carTrip.setUnloadingHascard(hascard);
							}
							Car query = new Car();
			            	query.setNumberPlate(prevCarHistory.getCarNumber());
			            	List<Car> cars = carMapper.select(query);
			            	if (cars.size() > 0) {
			            		carTrip.setDriverName(cars.get(0).getCarOwnersName());
			            	}
			            	carTrip.setInsertTime(new Date());
			            	carTrip.setEndTime(startTime.toDate());
			            	if (startTime.getHourOfDay() >= 0 && startTime.getHourOfDay() <= 6) {
			            		Date tripDate = DateTime.parse(startTime.minusDays(1).toString("yyyy-MM-dd"),
			            				DateTimeFormat.forPattern("yyyy-MM-dd")).toDate();
			            		carTrip.setTripDate(tripDate);
			            	} else {
			            		Date tripDate = DateTime.parse(startTime.toString("yyyy-MM-dd"),
			            				DateTimeFormat.forPattern("yyyy-MM-dd")).toDate();
			            		carTrip.setTripDate(tripDate);
			            	}
			            	if (trip.getBoxUpTime() != null) {
								carTrip.setBoxupTime(trip.getBoxUpTime());
							}
							if (trip.getBoxUpPlace() != null) {
								carTrip.setBoxupPlace(trip.getBoxUpPlace());
							}
			            	carTripMapper.insertSelective(carTrip);
			            	carTripMapper.updateEndGps(carTrip.getTripId(), trip.getEnterUnloadingTemp().getGpsPosX() + " " + 
			            			trip.getEnterUnloadingTemp().getGpsPosY());
						}
					}
					trip.setEnterUnloadingTemp(null);
					trip.setEnterUnloadingId(null);
					trip.setLeaveUnloadingTemp(null);
				}
				
				// 计算举斗时间和地点，并记录
				if (prevCarHistory.getBoxUp() == 2 && currCarHistory.getBoxUp() == 1) {
					trip.setBoxUpTime(prevCarHistory.getReceiveTime());
					CloseableHttpResponse httpResponse = null;
					try {
						CloseableHttpClient httpclient = HttpClients.createDefault();
					    RequestConfig requestConfig = RequestConfig.custom()
					    		.setSocketTimeout(5000)  
					            .setConnectTimeout(5000)  
					            .setConnectionRequestTimeout(5000)
					            .build();
						URI uri = new URIBuilder()
						        .setScheme("http")
						        .setHost("api.map.baidu.com")
						        .setPath("/geocoder/v2/")
						        .setParameter("ak", "nrWVaLzg4APuvhgFm4x5NDG2")
						        .setParameter("output", "json")
						        .setParameter("pois", "1")
						        .setParameter("location", prevCarHistory.getGpsPosY() + "," + prevCarHistory.getGpsPosX())
						        .build();
						HttpGet httpget = new HttpGet(uri);
						httpget.setConfig(requestConfig);
						httpResponse = httpclient.execute(httpget);
						ObjectMapper objectMapper = new ObjectMapper();
			    		Map<?, ?> map = objectMapper.readValue(EntityUtils.toString(httpResponse.getEntity()), HashMap.class);
			    		if (map.get("result") != null && ((Map<?, ?>) map.get("result")).get("formatted_address") != null) {
			    			trip.setBoxUpPlace((String) ((Map<?, ?>) map.get("result")).get("formatted_address"));
			    		}
					} catch (URISyntaxException | IOException e) {
						e.printStackTrace();
					} finally {
						if (httpResponse != null) {
							try {
								httpResponse.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					}
				}
				
				// 如果carTrip不为空则计算里程
				if (trip.getCarTrip() != null) {
					trip.getCarTrip().setMileage(trip.getCarTrip().getMileage() + GeoUtil.getDistance(prevPoint, currPoint).intValue());
	            	if (currCarHistory.getGpsSpeed() > trip.getCarTrip().getMaxSpeed()) {
	            		trip.getCarTrip().setMaxSpeed(currCarHistory.getGpsSpeed());
					}
				}
			}
		}
		
		/*//如果2个小时还没有计算完一趟则抛弃这一趟
		if (trip.getStartHistory() != null) {
			if (new DateTime(trip.getStartHistory().getReceiveTime()).plusHours(2).isBefore(DateTime.now())) {
				trip.setCarTrip(null);
				trip.setStartHistory(null);
			}
		}
		
		//开始循环计算每个历史轨迹点
		for (int i = 0; i < carHistorys.size(); i++) {
			CarHistory prevCarHistory = null;
			CarHistory currCarHistory = carHistorys.get(i);
			if (i == 0) {
				prevCarHistory = lastCarHistory;
			} else {
				prevCarHistory = carHistorys.get(i - 1);
			}
			if (currCarHistory.getBoxEmpty() == 2) {
				trip.setHeavyNumber(trip.getHeavyNumber() + 1);
				if (prevCarHistory != null && prevCarHistory.getBoxEmpty() == 1) {
					if (trip.getEmptyNumber() > 10) {
						trip.setStartHistory(prevCarHistory);
						
						CarTrip carTrip = new CarTrip();
						carTrip.setCarNumber(carAnalysis.getCarNumber());
						carTrip.setSimNumber(carAnalysis.getDevPhone());
						Iterator<Map.Entry<Long, List<Point>>> it = accessData.getFenceConstant().getConsappMap().entrySet().iterator();  
				        while (it.hasNext()) {
				        	Map.Entry<Long, List<Point>> entry = it.next();  
				            if (GeoUtil.containsPoint(entry.getValue(), 
				            		new Point(prevCarHistory.getGeo().getX(), prevCarHistory.getGeo().getY())) || 
				            	GeoUtil.containsPoint(entry.getValue(), 
						            new Point(currCarHistory.getGeo().getX(), currCarHistory.getGeo().getY()))) {
				            	carTrip.setConsappId(entry.getKey());
				            	break;
				            }
				        }
				        Car query = new Car();
		            	query.setNumberPlate(prevCarHistory.getCarNumber());
		            	List<Car> cars = carMapper.select(query);
		            	if (cars.size() > 0) {
		            		carTrip.setDriverName(cars.get(0).getCarOwnersName());
		            	}
		            	carTrip.setStartTime(new Date(prevCarHistory.getReceiveTime()));
		            	if (new DateTime(prevCarHistory.getReceiveTime()).getHourOfDay() >= 0 && 
		            			new DateTime(prevCarHistory.getReceiveTime()).getHourOfDay() <= 6) {
		            		Date tripDate = DateTime.parse(new DateTime(prevCarHistory.getReceiveTime()).minusDays(1).toString("yyyy-MM-dd"),
		            				DateTimeFormat.forPattern("yyyy-MM-dd")).toDate();
		            		carTrip.setTripDate(tripDate);
		            	} else {
		            		Date tripDate = DateTime.parse(new DateTime(prevCarHistory.getReceiveTime()).toString("yyyy-MM-dd"),
		            				DateTimeFormat.forPattern("yyyy-MM-dd")).toDate();
		            		carTrip.setTripDate(tripDate);
		            	}
		            	Point p1 = new Point(prevCarHistory.getGeo().getX(), prevCarHistory.getGeo().getY());
						Point p2 = new Point(currCarHistory.getGeo().getX(), currCarHistory.getGeo().getY());
		            	carTrip.setMileage(GeoUtil.getDistance(p1, p2).intValue());
		            	if (currCarHistory.getGpsSpeed() > prevCarHistory.getGpsSpeed()) {
		            		carTrip.setMaxSpeed(currCarHistory.getGpsSpeed());
						} else {
							carTrip.setMaxSpeed(prevCarHistory.getGpsSpeed());
						}
		            	trip.setCarTrip(carTrip);
					}
				} else if (prevCarHistory != null && prevCarHistory.getBoxEmpty() == 2) {
					if (trip.getCarTrip() != null) {
						Point p1 = new Point(prevCarHistory.getGeo().getX(), prevCarHistory.getGeo().getY());
						Point p2 = new Point(currCarHistory.getGeo().getX(), currCarHistory.getGeo().getY());
						trip.getCarTrip().setMileage(trip.getCarTrip().getMileage() + GeoUtil.getDistance(p1, p2).intValue());
		            	if (currCarHistory.getGpsSpeed() > prevCarHistory.getGpsSpeed()) {
		            		trip.getCarTrip().setMaxSpeed(currCarHistory.getGpsSpeed());
						} else {
							trip.getCarTrip().setMaxSpeed(prevCarHistory.getGpsSpeed());
						}
					}
				}
				trip.setEmptyNumber(0);
			} else if (currCarHistory.getBoxEmpty() == 1) {
				trip.setEmptyNumber(trip.getEmptyNumber() + 1);
				if (prevCarHistory != null && prevCarHistory.getBoxEmpty() == 2) {
					if (trip.getHeavyNumber() > 20) {
						if (trip.getCarTrip() != null &&
							(prevCarHistory.getBoxUp() == 2 || currCarHistory.getBoxUp() ==  2 || 
							 prevCarHistory.getBoxUp() == 3 || currCarHistory.getBoxUp() ==  3)) {
							CarTrip carTrip = trip.getCarTrip();
							Iterator<Map.Entry<Long, List<Point>>> it = accessData.getFenceConstant().getUnloadingMap().entrySet().iterator();
				        	while (it.hasNext()) {
				        		Map.Entry<Long, List<Point>> entry = it.next();
				        		if (GeoUtil.containsPoint(entry.getValue(), 
					            		new Point(prevCarHistory.getGeo().getX(), prevCarHistory.getGeo().getY())) || 
						            	GeoUtil.containsPoint(entry.getValue(), 
								            new Point(currCarHistory.getGeo().getX(), currCarHistory.getGeo().getY()))) {
				        			carTrip.setUnloadingId(entry.getKey());
				        			break;
				        		}
				        	}
				        	carTrip.setInsertTime(new Date());
				        	carTrip.setEndTime(new Date(currCarHistory.getReceiveTime()));
				        	Integer tripSecond = Seconds.secondsBetween(new DateTime(carTrip.getStartTime()), new DateTime(currCarHistory.getReceiveTime())).getSeconds();
				        	carTrip.setTripSecond(tripSecond);
				        	Point p1 = new Point(prevCarHistory.getGeo().getX(), prevCarHistory.getGeo().getY());
							Point p2 = new Point(currCarHistory.getGeo().getX(), currCarHistory.getGeo().getY());
							trip.getCarTrip().setMileage(trip.getCarTrip().getMileage() + GeoUtil.getDistance(p1, p2).intValue());
							if (currCarHistory.getGpsSpeed() > trip.getCarTrip().getMaxSpeed()) {
								trip.getCarTrip().setMaxSpeed(currCarHistory.getGpsSpeed());
							}
	    					carTripMapper.insertSelective(carTrip);
	    					carTripMapper.updateStartGps(carTrip.getTripId(), trip.getStartHistory().getGeo().getX() + " " + trip.getStartHistory().getGeo().getY());
	    					carTripMapper.updateEndGps(carTrip.getTripId(), currCarHistory.getGeo().getX() + " " + currCarHistory.getGeo().getY());
						}
					}
					if (trip.getCarTrip() != null) {
						trip.setCarTrip(null);
					}
					if (trip.getStartHistory() != null) {
						trip.setStartHistory(null);
					}
				} else if (prevCarHistory != null && prevCarHistory.getBoxEmpty() == 1) {
					if (trip.getCarTrip() != null) {
						trip.setCarTrip(null);
					}
					if (trip.getStartHistory() != null) {
						trip.setStartHistory(null);
					}
				}
				trip.setHeavyNumber(0);
			}
		}*/
	}
	
}
